import numpy as np
from mcush import *
from mcush import Mcush
from numpy import info
import socket

from scipy.signal import find_peaks

s = Mcush.Mcush(PORT)
h = Max30102(s)
while True:
    data_red, data_ir = h.readFifo()
    '''
    
    '''
    data_spo2 = []
    i = 0
    while i < len(data_red):
        R = data_red[i] / data_ir[i]
        data = 110 - 25 * R
        data_spo2.append(data)
        i = i + 1
    data_spo2 = data_spo2[3:len(data_spo2)]
    x = np.linspace(0, 5000, len(data_spo2))
    print(np.array(data_spo2))
    data_h = np.fft.fft(data_red, n=None, axis=-1, norm=None)
    [pks, locs] = find_peaks(data_h, height=None, threshold=None, distance=5,
                             prominence=None, width=None, wlen=None, rel_height=None,
                             plateau_size=None)
    # print(pks)
    print("心率为:每分钟{}下".format(len(pks) / 2))

    # info( 'RED: %d   IRED: %d'% (data_red, data_ir) )